<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加权限</title>
    <link rel="stylesheet" href="{{ asset('layui/css/layui.css') }}">
    <script src="{{ asset('layui/layui.js') }}"></script>
    <style>
        body {
            padding: 20px;
        }
        .layui-form-item {
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
    <form class="layui-form" lay-filter="permissionForm">
        @csrf
        <div class="layui-form-item">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入权限名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">权限标识</label>
            <div class="layui-input-block">
                <input type="text" name="slug" required lay-verify="required" placeholder="请输入权限标识" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">HTTP方法</label>
            <div class="layui-input-block">
                <input type="text" name="http_method" placeholder="GET,POST,PUT,DELETE (多个用逗号分隔)" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">HTTP路径</label>
            <div class="layui-input-block">
                <input type="text" name="http_path" placeholder="例如: admin/auth/*" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入描述" class="layui-textarea"></textarea>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="permissionForm">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.jquery;
            
            // 表单提交
            form.on('submit(permissionForm)', function(data) {
                $.ajax({
                    url: '{{ route('admin.auth.permissions.store') }}',
                    type: 'POST',
                    data: data.field,
                    success: function(res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                // 关闭弹窗并刷新父页面
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function(xhr) {
                        // 处理验证错误
                        if (xhr.status === 422) {
                            var errors = xhr.responseJSON.errors;
                            var errorMsg = '';
                            
                            for (var key in errors) {
                                errorMsg += errors[key][0] + '<br>';
                            }
                            
                            layer.msg(errorMsg, {icon: 2});
                        } else {
                            layer.msg('服务器错误，请稍后重试', {icon: 2});
                        }
                    }
                });
                
                return false;
            });
        });
    </script>
</body>
</html> 